package com.glq.book.service;

import com.glq.book.dao.BookDao;
import com.glq.book.dao.OrderDao;
import com.glq.book.entity.Book;
import com.glq.book.entity.Order;
import com.glq.book.vo.OrderVO;

import java.util.List;

public class OrderService {

    private OrderDao orderDao = new OrderDao();
    private BookDao bookDao = new BookDao();

    /**
     * 添加订单
     * 减少书籍数量，然后添加订单，返回订单号
     *
     * @param bookName
     * @param username
     * @param number
     * @param money
     * @param bookId
     * @return
     */
    public boolean insertOrder(String bookName, String username, Integer number, Integer money, Integer bookId ,String orderId) {
        OrderVO order = new OrderVO(bookName, username, number, money);
        if (order.getMoney() < 0 ||
                order.getNumber() < 0 ||
                order.getUsername() == null ||
                order.getUsername() == "") {
            return false;
        }
        boolean b = bookDao.subBookNumber(bookId, order.getNumber());
        if (!b) {
            boolean b1 = orderDao.insertOrder(order, orderId);
            if (b1) {
                return true;
            }

        }
        return false;
    }

    /**
     * 获取当前用户的订单列表（用户）
     *
     * @param username
     * @return
     */
    public List<Order> selectOrderListByUser(String username) {
        return orderDao.selectOrderListByUser(username);
    }

    /**
     * 获取所有订单（管理员）
     *
     * @return
     */
    public List<Order> selectOrderList() {
        return orderDao.selectOrderList();
    }

    /**
     * 获取具体订单
     *
     * @param orderId
     * @return
     */
    public Order selectByOrderId(String orderId) {
        return orderDao.selectByOrderId(orderId);
    }
}
